﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using MaxStudio.Concrete.Component;
using System.Configuration; 

namespace MaxStudio.Concrete.Web.Pages
{
    /// <summary>
    /// 车队发货
    /// </summary>
    public partial class TransportProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
                BindMotocade(); 
             }
        }

        #region Private Method

        public string GetCustomerNameByContranctSN(string strContranctSN)
        {
            string strReturnvalue = "";
            SalesOrderService salesOrderServiceObj = ServiceFactory.getSalesOrderServices(); 
            OperationResult<tb_SalesOrder> result = salesOrderServiceObj.GetSalesOrderByContranctSN(strContranctSN);
            if (result.operationSuccess)
            {
                strReturnvalue = result.obj.tb_Customer.Name; 
            } 

            return strReturnvalue;
        }

        public string GetUnitStringByUnitId(string strUnit)
        {
            string strReturnvalue = "";
            if (strUnit == "0")
            {
                strReturnvalue = "立方";
            }
            if (strUnit == "1")
            {
                strReturnvalue = "吨";
            }
            return strReturnvalue;  
        }

        public string GetMotocardNameById(string strId)
        {
            string strReturnValue = "";
            MotorcadeService motorCadeServiceObj = ServiceFactory.getMotorcadeService();
            OperationResult<tb_Motorcade> resut = motorCadeServiceObj.GetMotorcadeById(Int32.Parse(strId));
            if (resut.operationSuccess)
            {
                strReturnValue = resut.obj.Name;
            }

            return strReturnValue;
        }

        private void BindMotocade()
        {
            MotorcadeService motorcadeServiceObj = ServiceFactory.getMotorcadeService();
            OperationResult<List<tb_Motorcade>> result = motorcadeServiceObj.GetAllMotorcade();
            if (result.operationSuccess)
            {
                ddlMotocadeId.DataSource = result.obj;
                ddlMotocadeId.DataTextField = "Name";
                ddlMotocadeId.DataValueField = "Id";
                ddlMotocadeId.DataBind();
            }
        }

        private void BindEditMotocade()
        {
            MotorcadeService motorcadeServiceObj = ServiceFactory.getMotorcadeService();
            OperationResult<List<tb_Motorcade>> result = motorcadeServiceObj.GetAllMotorcade();
            if (result.operationSuccess)
            {
                ddlEditMotocadeId.DataSource = result.obj;
                ddlEditMotocadeId.DataTextField = "Name";
                ddlEditMotocadeId.DataValueField = "Id";
                ddlEditMotocadeId.DataBind();
            }
        }

        private void BindGridView()
        {
            TransportService transportServiceObj = ServiceFactory.getTransportService();
            OperationResult<List<tb_Transport>> result = transportServiceObj.GetTransportByTransportType(Int32.Parse(ConfigurationSettings.AppSettings["TransportProductType"].ToString()));

            if (result.operationSuccess)
            {
                gvTransportProductList.DataSource = result.obj;
                gvTransportProductList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            BindEditMotocade();

            TransportService transportServiceObj = ServiceFactory.getTransportService();

            OperationResult<tb_Transport> resultEdit = transportServiceObj.GetTransportById(id);
            if (resultEdit.operationSuccess == true)
            {

                txtEditReferId.Text = resultEdit.obj.ReferId.ToString();
                txtEditCount.Text = resultEdit.obj.Count.ToString();
                ddlEditCountType.SelectedValue = resultEdit.obj.CountUnit.ToString();
                ddlEditMotocadeId.Text = resultEdit.obj.MotocadeId.ToString();
                txtEditLoadingDate.Text = resultEdit.obj.LoadingDate.ToShortDateString() ;
              

                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();
            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        public string GetMasterDataNameById(string strTypeId)
        {
            int masterDataId = Int32.Parse(strTypeId);
            return MasterDateString.GetMasterDataNameById(masterDataId);
        }
        #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            TransportService transportServiceObj = ServiceFactory.getTransportService(); 
            tb_Transport instance = new tb_Transport();

            instance.ReferId = Int32.Parse ( txtReferId.Text);
            instance.Count = decimal.Parse(txtCount.Text);
            instance.CountUnit = Int32.Parse(ddlCountType.SelectedValue);
            instance.MotocadeId = Int32.Parse(ddlMotocadeId.SelectedValue);
            instance.LoadingDate = DateTime.Parse(txtLoadingDate.Text);
            instance.TransportType =Int32.Parse(ConfigurationSettings.AppSettings["TransportProductType"].ToString ());

            OperationResult<tb_Transport> result = transportServiceObj.CreateTransport(instance);
            BindGridView();
        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }
            TransportService transportServiceObj = ServiceFactory.getTransportService();

            tb_Transport entity = new tb_Transport();

            entity.Id = editId;

            entity.ReferId =  Int32.Parse ( txtEditReferId.Text);
            entity.Count =decimal.Parse( txtEditCount.Text);
            entity.CountUnit = Int32.Parse(ddlEditCountType.SelectedValue);
            entity.MotocadeId = Int32.Parse(ddlEditMotocadeId.SelectedValue);
            entity.LoadingDate = DateTime.Parse(txtEditLoadingDate.Text);
            entity.TransportType = Int32.Parse(ConfigurationManager.AppSettings["TransportProductType"].ToString ());

            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_Transport> resultEdit = transportServiceObj.ModifyTransport(entity);
            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            TransportService transportServiceObj = ServiceFactory.getTransportService();
            tb_Transport deleteEntity = new tb_Transport();
            deleteEntity.Id = deleteId;
            transportServiceObj.DeleteTransport(deleteEntity);
            BindGridView();
        }

        protected void gvTransportProductList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvTransportProductList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvTransportProductList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion
    }
}
